import { categoryListApi } from '@/api/category'
// 递归处理分类列表中是否可用字段。（->boolean）
function formatIsShow(data) {
    if (data instanceof Array) {
        data.forEach(i => {
            i.is_show = Boolean(i.is_show - 0)
            if (i.children) formatIsShow(i.children)
        });
    }
    return
}
export default {
    state: {
        categoryList: [],
    },
    mutations: {
        saveCategoryList(state, categoryList) {
            formatIsShow(categoryList)
            state.categoryList = categoryList
        }
    },
    actions: {
        async saveCategoryList(context, level) {
            try {
                const result = await categoryListApi(level)
                context.commit('saveCategoryList', result.data.data.data)
            } catch (e) {
                console.log(e.response);
            }
        },
    },
}